package com.puff.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.puff.constant.MessageConstant;
import com.puff.constant.StatusConstant;
import com.puff.dto.ShopDTO;
import com.puff.dto.ShopListDTO;
import com.puff.dto.ShopPageQueryDTO;
import com.puff.entity.Dish;
import com.puff.entity.Employee;
import com.puff.entity.Shop;
import com.puff.entity.ShopCategory;
import com.puff.exception.DeletionNotAllowedException;
import com.puff.mapper.ShopCategoryMapper;
import com.puff.mapper.ShopMapper;
import com.puff.result.PageResult;
import com.puff.service.ShopService;
import com.puff.vo.ShopVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
@Slf4j
public class ShopServiceImpl implements ShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private ShopCategoryMapper shopCategoryMapper;

    /**
     * 平台端商家分页查询
     * @param shopPageQueryDTO
     * @return
     */
    public PageResult pageQuery(ShopPageQueryDTO shopPageQueryDTO) {
        // 利用PageHelper进行分页查询的参数计算
        PageHelper.startPage(shopPageQueryDTO.getPage(),shopPageQueryDTO.getPageSize());

        // 动态获取查询条件（员工名称），进行条件查询
        Page<ShopVO> page = shopMapper.pageQuery(shopPageQueryDTO);

        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 新增商家
     * @param shopDTO
     */
    public void saveShop(ShopDTO shopDTO) {
        Shop shop = new Shop();
        BeanUtils.copyProperties(shopDTO,shop);

        // 向商家表中插入一条数据
        shopMapper.insert(shop);
    }

    /**
     * 批量删除商家信息
     * @param ids
     */
    public void deleteBatch(List<Long> ids) {
        // 判断当前菜品是否能够删除 -- 是否存在起售中的菜品
        for (Long id : ids) {
            Shop shop = shopMapper.getById(id);
            if (shop.getStatus() == StatusConstant.ENABLE){
                // 如果是营业中状态，则不能删除
                throw new DeletionNotAllowedException(MessageConstant.SHOP_OPERATING);
            }
        }

        // 根据菜品id集合批量删除菜品数据
        shopMapper.deleteByIds(ids);
    }

    /**
     * 根据id查询菜品信息
     * @param id
     * @return
     */
    public ShopVO getShopById(Long id) {
        Shop shop = shopMapper.getById(id);

        ShopCategory shopCategory = shopCategoryMapper.getById(shop.getShopCategoryId());


        ShopVO shopVO = new ShopVO();
        BeanUtils.copyProperties(shop,shopVO);
        shopVO.setShopCategoryName(shopCategory.getName());
        return shopVO;
    }

    /**
     * 修改商家信息
     * @param shopDTO
     */
    public void updateShop(ShopDTO shopDTO) {
        Shop shop = new Shop();
        BeanUtils.copyProperties(shopDTO,shop);

        shopMapper.update(shop);
    }

    /**
     * 商家打烊中/营业中切换
     * @param id
     * @param status
     */
    public void startOrStopShop(Long id, Integer status) {
        Shop shop = Shop.builder()
                        .id(id)
                        .status(status)
                        .build();
        shopMapper.update(shop);
    }

    /**
     * 用户端商家查询
     * query查询
     * @param shopListDTO
     * @return
     */
    public List<ShopVO> list(ShopListDTO shopListDTO) {
        return shopMapper.list(shopListDTO);
    }
}
